package com.jqweb.controller.User;

import com.jqweb.controller.User.pojo.LoginDto;
import com.jqweb.controller.User.pojo.RegisterDto;
import com.jqweb.entity.AddressEntity;
import com.jqweb.entity.UserEntity;
import com.jqweb.pojo.Result;
import com.jqweb.services.UserService;
import com.jqweb.utils.JWT;
import org.noear.solon.annotation.Header;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class userController {
    @Autowired
    private UserService userService;

    /**
     * 用户登录接口
     * @param userInfo 用户信息参数
     */
    @PostMapping(value = "/login")
    public Result login (@RequestBody LoginDto userInfo) {
        Map<String, Object> login = userService.Login(userInfo);
        if (login == null) {
            return Result.error("登录失败");
        }

        return Result.success("登录成功",login);
    }

    // 管理员登陆接口
    @PostMapping(value = "/admin/login")
    public Result managerLogin (@RequestBody LoginDto userInfo) {
        Map<String, Object> login = userService.managerLogin(userInfo);
        if (login == null) {
            return Result.error("登录失败");
        }
        return Result.success("登录成功",login);
    }

    // 管理员新增用户
    @PostMapping("/admin/register")
    public Result registerAdmin(@RequestBody RegisterDto userInfo) {
        Integer register = userService.addUser(userInfo);
        if (register == null){
            return Result.error("新增失败");
        }
        return Result.success("新增成功",register);
    }

    // 注册接口
    @PostMapping(value = "/register")
    public Result register (@RequestBody RegisterDto userInfo) {
        Map<String,Object> register = userService.Register(userInfo);
        if (register == null){
            return Result.error("注册失败");
        }
        return Result.success("注册成功",register);
    }

    // 获取所有用户信息
    @GetMapping("/getAll")
    public Result getAll() {
        List<UserEntity> all = userService.getAllUser();
        return Result.success("获取成功", all);
    }

    // 删除用户
    @GetMapping("/delete/{id}")
    public Result deleteUser(@PathVariable Integer id) {
        int i = userService.deleteUser(id);
        if (i == 0) {
            return Result.error("删除失败");
        }
        return Result.success("删除成功", i);
    }

    // 获取用户地址信息列表
    @GetMapping("/getAddress/{id}")
    public Result getAddress(@PathVariable Integer id) {
        List<AddressEntity> address = userService.getUserAddress(id);
        return Result.success("获取成功", address);
    }

    // 新增地址
    @PostMapping("/addAddress")
    public Result addAddress(@RequestBody AddressEntity address) {
        int i = userService.addAddress(address);
        if (i == 0) {
            return Result.error("新增失败");
        }
        return Result.success("新增成功", null);
    }

    // 删除地址
    @GetMapping("/deleteAddress/{id}")
    public Result deleteAddress(@PathVariable Integer id) {
        int i = userService.deleteAddress(id);
        if (i == 0) {
            return Result.error("删除失败");
        }
        return Result.success("删除成功", null);
    }

    // 更新地址
    @PostMapping("/updateAddress")
    public Result updateAddress(@RequestBody AddressEntity address) {
        int i = userService.updateAddress(address);
        if (i == 0) {
            return Result.error("更新失败");
        }
        return Result.success("更新成功", null);
    }

    // 修改用户信息
    @PostMapping("/update")
    public Result update(@RequestBody UserEntity userEntity) {
        Integer i = userService.updateUser(userEntity);
        if (i == 0) {
            return Result.error("更新失败");
        }
        return Result.success("更新成功", null);
    }

    // 根据token信息获取用户信息
    @GetMapping("/getUserInfo")
    public Result getUserInfo(@RequestHeader(value = "Authorization", required = false) String token) {
        Object id = JWT.getClaimValue(token,"id");
        if (id == null){
            return Result.error("token失效");
        }
        UserEntity userInfo = userService.getUserInfo((String) id);
        if (userInfo == null) {
            return Result.error("获取失败");
        }
        return Result.success("获取成功", userInfo);
    }
}
